package com.rockontrol.ele.service.model.dbtransaction.impl;

import java.math.BigDecimal;
import java.util.List;

import com.rksp.svc.BaseSvc;
import com.rockontrol.ele.pojo.Ma帐套;
import com.rockontrol.ele.pojo.Ma火工品台帐明细;
import com.rockontrol.ele.pojo.Ma火工品消耗台帐;
import com.rockontrol.ele.pojo.Ma火工品消耗台帐明细;
import com.rockontrol.ele.pojo.Ma火工品登记台帐;
import com.rockontrol.ele.pojo.Ma火工品雷管消耗明细;
import com.rockontrol.ele.pojo.Ma物资库存;
import com.rockontrol.ele.service.model.dbtransaction.MaGunpowderTrans;

public class MaGunpowderTransImpl extends BaseSvc implements MaGunpowderTrans {
	
	private Ma帐套 getAccount(){
		List<Ma帐套> list=service.findObjsHQL("from Ma帐套 a where a.台帐名称 = '井下炸药库'");
		return list==null||list.isEmpty()?null:list.get(0);
	}
	
	public String serviceCreateTheGunpowderRegister(Ma火工品登记台帐 ia_) {
		service.addObj(ia_);
		Ma帐套 ma帐套=getAccount();
		if(ia_.getHyList()!=null&&!ia_.getHyList().isEmpty()){
			for(Ma火工品台帐明细 detail:ia_.getHyList()){
				//增加没有的物资库存量
				List<Ma物资库存> list=service.findObjsHQL("from Ma物资库存 a where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				if(list==null||list.isEmpty()){
					Ma物资库存 ma物资库存=new Ma物资库存();
					ma物资库存.set帐套id(ma帐套.getOid());
					ma物资库存.set分类编码("");
					ma物资库存.set单价(new BigDecimal(0));
					ma物资库存.set单位(detail.get单位());
					ma物资库存.set厂商id("1");
					ma物资库存.set厂商名称("");
					ma物资库存.set台帐单位(ma帐套.get部门单位());
					ma物资库存.set台帐单位id(ma帐套.get单位id());
					ma物资库存.set台帐单位编码(ma帐套.get单位编码());
					ma物资库存.set库存量(new BigDecimal(0.0));
					ma物资库存.set录入人(ia_.get录入人());
					ma物资库存.set录入人id(ia_.get录入人());
					ma物资库存.set录入时间(ia_.get录入日期());
					ma物资库存.set物资分类(detail.get物资分类名称());
					ma物资库存.set物资分类id(detail.get物资分类id());
					ma物资库存.set规格型号(detail.get物资型号名称());
					ma物资库存.set规格型号id(detail.get物资型号id());
					ma物资库存.set规格流水号("");
					ma物资库存.set阀值上限(null);
					ma物资库存.set阀值下限(null);
					service.addObj(ma物资库存);
				}
				List<Ma物资库存> ll=service.findObjsHQL("from Ma物资库存 a where  a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				Ma物资库存 ma物资库存=ll==null||ll.isEmpty()?null:ll.get(0);
				ma物资库存.set库存量(new BigDecimal(ma物资库存.get库存量().doubleValue()+detail.get数量().doubleValue()));
				service.modifyObj(ma物资库存);
				if(ma物资库存!=null){
					detail.set库存量(ma物资库存.get库存量());
				}
				detail.set台帐id(ia_.getOid());
				service.addObj(detail);
			}
		}
		if(ia_.getLgList()!=null&&!ia_.getLgList().isEmpty()){
			for(Ma火工品台帐明细 detail:ia_.getLgList()){
				//增加没有的物资库存量
				List<Ma物资库存> list=service.findObjsHQL("from Ma物资库存 a where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				if(list==null||list.isEmpty()){
					Ma物资库存 ma物资库存=new Ma物资库存();
					ma物资库存.set帐套id(ma帐套.getOid());
					ma物资库存.set分类编码("");
					ma物资库存.set单价(new BigDecimal(0));
					ma物资库存.set单位(detail.get单位());
					ma物资库存.set厂商id("1");
					ma物资库存.set厂商名称("");
					ma物资库存.set台帐单位(ma帐套.get部门单位());
					ma物资库存.set台帐单位id(ma帐套.get单位id());
					ma物资库存.set台帐单位编码(ma帐套.get单位编码());
					ma物资库存.set库存量(new BigDecimal(0.0));
					ma物资库存.set录入人(ia_.get录入人());
					ma物资库存.set录入人id(ia_.get录入人());
					ma物资库存.set录入时间(ia_.get录入日期());
					ma物资库存.set物资分类(detail.get物资分类名称());
					ma物资库存.set物资分类id(detail.get物资分类id());
					ma物资库存.set规格型号(detail.get物资型号名称());
					ma物资库存.set规格型号id(detail.get物资型号id());
					ma物资库存.set规格流水号("");
					ma物资库存.set阀值上限(null);
					ma物资库存.set阀值下限(null);
					service.addObj(ma物资库存);
				}
				List<Ma物资库存> ll=service.findObjsHQL("from Ma物资库存 a where  a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				Ma物资库存 ma物资库存=ll==null||ll.isEmpty()?null:ll.get(0);
				ma物资库存.set库存量(new BigDecimal(ma物资库存.get库存量().doubleValue()+detail.get数量().doubleValue()));
				service.modifyObj(ma物资库存);
				if(ma物资库存!=null){
					detail.set库存量(ma物资库存.get库存量());
				}
				detail.set台帐id(ia_.getOid());
				service.addObj(detail);
			}
		}
		return ia_.getOid();
	}
	
	public Boolean serviceUpdateTheGunpowderRegister(Ma火工品登记台帐 ia_) {

		service.execHQL("delete from Ma火工品台帐明细 a where a.台帐id='"+ia_.getOid()+"'");
		if(ia_.getHyList()!=null&&!ia_.getHyList().isEmpty()){
			for(Ma火工品台帐明细 detail:ia_.getHyList()){
				detail.set台帐id(ia_.getOid());
				service.addObj(detail);
			}
		}
		if(ia_.getLgList()!=null&&!ia_.getLgList().isEmpty()){
			for(Ma火工品台帐明细 detail:ia_.getLgList()){
				detail.set台帐id(ia_.getOid());
				service.addObj(detail);
			}
		}
		service.modifyObj(ia_);
		return true;
	}
	
	public Boolean serviceDeleteTheGunpowderRegister(String id_) {
		service.execHQL("delete from Ma火工品台帐明细 a where a.台帐id='"+id_+"'");
		service.removeObj(Ma火工品登记台帐.class, id_);
		return true;
	}

	public String serviceCreateTheConsumeRegister(Ma火工品消耗台帐 oisr_) {
		service.addObj(oisr_);
		Ma帐套 ma帐套=getAccount();
		if(oisr_.getFhyList()!=null&&!oisr_.getFhyList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getFhyList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				service.execHQL("update Ma物资库存 a set a.库存量=a.库存量-"+detail.get数量()+" where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getFlgList()!=null&&!oisr_.getFlgList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getFlgList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				service.execHQL("update Ma物资库存 a set a.库存量=a.库存量-"+detail.get数量()+" where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getThyList()!=null&&!oisr_.getThyList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getThyList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				service.execHQL("update Ma物资库存 a set a.库存量=a.库存量-"+detail.get数量()+" where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getTlgList()!=null&&!oisr_.getTlgList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getTlgList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				service.execHQL("update Ma物资库存 a set a.库存量=a.库存量-"+detail.get数量()+" where a.帐套id='"+ma帐套.getOid()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get物资型号id()+"'");
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		return oisr_.getOid();
	}

	public Boolean serviceUpdateConsumeRegister(Ma火工品消耗台帐 oisr_) {
		service.execHQL("delete from Ma火工品消耗台帐明细 a where a.台帐id='"+oisr_.getOid()+"'");
		service.execHQL("delete from Ma火工品雷管消耗明细 a where a.明细id in (select oid from Ma火工品消耗台帐明细 b where b.台帐id='"+oisr_.getOid()+"')");
		if(oisr_.getFhyList()!=null&&!oisr_.getFhyList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getFhyList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getFlgList()!=null&&!oisr_.getFlgList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getFlgList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getThyList()!=null&&!oisr_.getThyList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getThyList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		if(oisr_.getTlgList()!=null&&!oisr_.getTlgList().isEmpty()){
			for(Ma火工品消耗台帐明细 detail:oisr_.getTlgList()){
				detail.set台帐id(oisr_.getOid());
				service.addObj(detail);
				if(detail.getDetail()!=null&&!detail.getDetail().isEmpty()){
					for(Ma火工品雷管消耗明细 lg:detail.getDetail()){
						lg.set明细id(detail.getOid());
						service.addObj(lg);
					}
				}
			}
		}
		service.modifyObj(oisr_);
		return true;
	}


	public Boolean serviceDeleteTheConsumeRegister(String id_) {
		service.execHQL("delete from Ma火工品消耗台帐明细 a where a.台帐id='"+id_+"'");
		service.execHQL("delete from Ma火工品雷管消耗明细 a where a.明细id in (select oid from Ma火工品消耗台帐明细 b where b.台帐id='"+id_+"')");
		service.removeObj(Ma火工品消耗台帐.class, id_);
		return true;
	}


}
